package com.ParkingManager.app.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.ParkingManager.app.carPlateRecognition.carPlateRecognition;
import com.ParkingManager.app.service.ImgService;
import com.ParkingManager.app.util.PathManager;

@Service
public class ImgServiceImpl implements ImgService {


	public String[] doLPR(String filePath) {
		// TODO Auto-generated method stub
		String[] carId = carPlateRecognition.carPlateRec(filePath);
//		System.out.println(carId[0]);
//		System.out.println(carId[1]);
		return carId;
	}


	public String saveImg(MultipartFile uploadImage) {
		// TODO Auto-generated method stub
		//生成随机文件名
		System.out.println(uploadImage.getOriginalFilename());
		SimpleDateFormat simpleDateFormat;
		simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
		Date date = new Date();
		String str = simpleDateFormat.format(date);
		Random random = new Random();
		int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
		String fileName = rannum + str + ".jpg";
		System.out.println(fileName);
		//文件格式转换并写入
		try {
			InputStream inputStream = uploadImage.getInputStream();
			OutputStream outputFile = new FileOutputStream(
				PathManager.imagePath
				+File.separator
				+fileName
			);
			byte[] buffer = new byte[1024];
			int len;
			//inputStream转fileOutputStream
			while ((len = inputStream.read(buffer)) != -1) {
				outputFile.write(buffer, 0, len);
            }
			outputFile.close();
			inputStream.close();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return fileName;
	}


	public String getImg() {
		// TODO Auto-generated method stub
		return null;
	}

}
